package com.example.carRental.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.carRental.entity.Car;
import com.example.carRental.entity.Customer;
import com.example.carRental.entity.Order;
import com.example.carRental.entity.User;
import com.example.carRental.mapper.CarMapper;
import com.example.carRental.mapper.CustomerMapper;
import com.example.carRental.mapper.OrderMapper;
import com.example.carRental.rabbitmqDealUtils.DealCode;
import com.example.carRental.rabbitmqDealUtils.DealMessage;
import com.example.carRental.service.CustomerService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

/**
 * @author 92307
 * @version 1.0
 * @time 2022/3/29
 * @Describe describe
 */
@PreAuthorize("hasAnyAuthority('user')")
@Service
public class CustomerServiceImpl implements CustomerService {
    @Resource
    private CustomerMapper customerMapper;
    @Resource
    private CarMapper carMapper;
    @Resource
    private OrderMapper orderMapper;
    //    连接rabbitmq
    @Resource
    private RabbitTemplate rabbitTemplate;
    private String exchangeName = "order_exchange";
    private String routeKeySMS = "sms";
    private String routeKeyEmail = "email";




    @Override
    public Customer getInfo() {
        return customerMapper.selectById(User.getUserId());
    }

    @Override
    public List<Car> getAllCar() {
        List<Car> allCar = carMapper.getAllCar();
        for (Car car : allCar) {
            car.setImage(carMapper.getPictures(car.getId()));
        }
        return allCar;
    }

    /**
     * @Description: 通过汽车Id和租用天数来生成订单。同时给用户、出租房发送租用成功信息。给用户发送email信息。
     * @Author: lpf
     * @Date: 2022/4/2
     */
    @Override
    public void addOrder(int carId, int days) {
        Order entity = new Order(null, User.getUserId(), carId, 0, days, getTotalPrice(carId, days), LocalDate.now());
        orderMapper.insertOrder(entity);
//        给用户发信息
//        rabbitTemplate.convertAndSend(exchangeName,routeKeySMS,new DealMessage(DealCode.sentMessageToCustomer,User.getUserId()));
        Integer chartererId = carMapper.getChartererIdById(carId);
//        给商户发信息
//             rabbitTemplate.convertAndSend(exchangeName,routeKeySMS, new DealMessage(DealCode.sentMessageToCharterer,chartererId));
//        给商户发邮件
        rabbitTemplate.convertAndSend(exchangeName,routeKeyEmail,new DealMessage(DealCode.sentMessageToCustomer,chartererId));
        carMapper.setCarType(carId);
    }

    /**
     * @Description: 获得应付价格，可以加入优惠券。
     * @Author: lpf
     * @Date: 2022/4/2
     */
    private Integer getTotalPrice(Integer carId,Integer days){
        Integer carPrice = carMapper.getPriceByCarId(carId);
        return carPrice*days;
    }

    /**
     * @Description: 修改个人信息
     * @Author: lpf
     * @Date: 2022/4/10
     */
    @Override
    public void updateInfo(Customer customer) {
        QueryWrapper<Customer> customerQueryWrapper = new QueryWrapper<>();
        customerQueryWrapper.eq("id",customer.getId());
        customerMapper.update(customer,customerQueryWrapper);
    }

    /**
     * @Description: 获得所有自己的订单。
     * @Author: lpf
     * @Date: 2022/4/10
     */
    @Override
    public List<Order>  orderList() {
        Customer info = getInfo();
        List<Order> orders = orderMapper.getOrderByCustomerId(info.getId());
        return orders;
    }

}
